<?php

class CView {
	
	private static $instance = null;

	private		$vars		 = array();

	protected	$theme		 = '';

	public function __construct() {
		
	}

	public function setTheme($theme = 'default') {
		$this->theme	= $theme;
	}
	
	public static function getInstance() {
		if(!self::$instance) {
			self::$instance = new CView();
		}
		return self::$instance;
	}
	
	public function assign($name,$value) {
		if(is_array($name)) {
			$this->vars = array_merge($this->vars,$name);
		} else {
			$this->vars[$name] = $value;
		}
	}
	
	
	public function template($file) {
		if(!$file) {//如果文件不存在，使用默认
			$file =  C("TEMPLATE_NAME");
		} else {
			$path =  explode("/",trim($file,"/"));
			$a	  = array_pop($path);
			$c	  = empty($path) ? CAPP::getControllerName() : array_pop($path);
			$file = $c . '/' . $a;
		}
		$file	 = (empty($this->theme) ? '' : $this->theme . '/') . $file;
		
		//把app name作为生成缓存文件的prefix;
		
		$tpl	 = CPATH_DATATPL . '/' .( defined("APP_NAME") ? APP_NAME.'_' : '') . str_replace("/","_",$file) . '.php';
		$file	 = CPATH_VIEW.'/'.$file.'.html';

		if(!is_file($file)) {//如果文件的原始文件不存在，直接halt
			halt(L("FILE_NOT_EXIST",$file));
		}

//		if(is_file($tpl) && filemtime($tpl) > filemtime($file)) {//如果文件已经存在，且未过期
//			return $tpl;
//		}
	
		$content = file_get_contents($file);
		$content = preg_replace("/<\!\-\-\{(.+?)\}\-\->/is","{\\1}",$content);

		//模版替换函数
		$content = preg_replace("/\{template\s*\(?(.+?)\)?\}/is",		"<?php include \$this->template(\\1); ?>",$content);
		$content = preg_replace("/\{php\s+(.+?)\}/is",					"<?php \\1; ?>",			$content);


		//替换if语句
		$content = preg_replace("/\{if\s+(.+?)\}/is",					"<?php if(\\1) { ?>",		$content);
		$content = preg_replace("/\{else\s*\}/is",						"<?php } else { ?>",		$content);
		$content = preg_replace("/\{elseif\s+(.+?)\s*\}/is",			"<?php } elseif(\\1) { ?>", $content);
		$content = preg_replace("/\{\/if\s*\}/is",						"<?php } ?>",				$content);

		//替换for
		$content = preg_replace("/\{for\s*\((.+?)\)\s*\}/is",			"<?php for(\\1) { >",$content);
		$content = preg_replace("/\{\/for\}/is",						"<?php } ?>",$content);

		//替换loop
		$content = preg_replace("/\{(?:foreach|loop)\s+(\S+)\s+(\S+)\}/is",			"<?php if(is_array(\\1)) { \$n = 1; foreach(\\1 as \\2) { ?>",$content);
		$content = preg_replace("/\{(?:foreach|loop)\s+(\S+)\s+(\S+)\s+(\S+)\}/is", "<?php if(is_array(\\1)) { \$n = 1; foreach(\\1 as \\2 => \\3) { ?>",$content);

		$content = preg_replace("/\{\/(?:foreach|loop)\}/is",						"<?php \$n++;} unset(\$n);  }  ?>",$content);

		//替换常量
		$content = preg_replace("/\{([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\[\]\$\'\"\x7f-\xff]*)\}/",	 "<?php echo \\1; ?>",$content);
		//替换变量
		$content = preg_replace("/\{(\\\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\[\]\$\'\"\x7f-\xff]*)\}/","<?php echo \\1; ?>",$content);
		//替换数组
		$content = preg_replace("/\{(\\\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\.([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\}/","<?php echo \\1['\\2']; ?>",$content);

		//php默认函数，或者自定义的函数
		$content = preg_replace("/\{([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\((.+?)\)\}/","<?php echo \\1(\\2); ?>",$content);
		
		//附加
		$content = "<?php defined('IN_CART') || die ;?>" . $content;
		file_put_contents($tpl,$content);
		return $tpl;
	}

	public function display($file = '') {
		$tpl  = $this->template($file);
		
		if($this->vars) {
			extract($this->vars,EXTR_OVERWRITE);
			$this->vars = array();
		}
		ob_start();
		include $tpl;
		$content = ob_get_clean();
		echo $content;
	}

}